﻿using Oracle.ManagedDataAccess.Client;
using System.Data;
using static System.Windows.Forms.AxHost;

namespace AccountOperations
{
    /*
     * AccountOperations类中包含账户操作的函数，函数中的所有的MessageBox显示信息都是暂时调试用的
     */
    public static class AccountOperations
    {
        public enum OperateNumber
        {
            freeze, // 冻结
            unfreeze, // 解冻
            accountClosure // 销户
        };

        private const string connectionString = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=47.96.39.153)(PORT=1521))(CONNECT_DATA=(SID=orcl)));
                                                 User Id=system;
                                                 Password=Tongji123456;
                                                 Connection Timeout=5;";

        // 检查银行卡是否存在
        public static bool CheckBankCard(string bankCardID)
        {
            DataTable dataTable = BankCardInquiry(bankCardID);
            if (dataTable.Rows.Count == 0)
            {
                return false;
            }
            else
            {
                DataRow row = dataTable.Rows[0];
                string state = row["STATE"].ToString();
                return state != "已销户";
            }
        }

        public static string BankCardStateInquiry(string bankCardID)
        {
            DataTable dataTable = BankCardInquiry(bankCardID);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                DataRow row = dataTable.Rows[0];
                string state = row["STATE"].ToString();
                return state == null ? "" : state;
            }
            else
            {
                return "";
            }
        }

        public static string BankCardBalanceInquiry(string bankCardID)
        {
            DataTable dataTable = BankCardInquiry(bankCardID);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                DataRow row = dataTable.Rows[0];
                string balance = row["BALANCE"].ToString();
                return balance == null ? "" : balance;
            }
            else
            {
                return "";
            }
        }

        // toUpdateBalance是更新后的余额
        public static bool BankCardBalanceToUpdate(string bankCardID, decimal toUpdateBalance)
        {
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 构建更新语句
                    string updateQuery = @"UPDATE ACCOUNT SET BALANCE = :toUpdateBalance 
                        WHERE CREDIT_CARD_NUMBERS = :bankCardID";
                    using (OracleCommand command = new OracleCommand(updateQuery, connection))
                    {
                        string newBalance = toUpdateBalance.ToString();
                        // 添加参数
                        command.Parameters.Add(":toUpdateBalance", OracleDbType.Varchar2).Value = newBalance;
                        command.Parameters.Add(":bankCardID", OracleDbType.Varchar2).Value = bankCardID;

                        // 执行更新操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        // updateScore是需要改变的数额，例如10000、-500
        public static bool UpdateCreditScore(string cardID, Int32 updateScore)
        {
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 构建更新语句
                    string updateQuery = @"UPDATE ACCOUNT SET CREDIT_SCORE = CREDIT_SCORE + :updateScore 
                        WHERE CREDIT_CARD_NUMBERS = :cardID";
                    using (OracleCommand command = new OracleCommand(updateQuery, connection))
                    {
                        // 添加参数
                        command.Parameters.Add(":updateScore", OracleDbType.Int32).Value = updateScore;
                        command.Parameters.Add(":cardID", OracleDbType.Varchar2).Value = cardID;

                        // 执行更新操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        // toUpdateBalance是需要改变的数额，例如10000、-500
        public static bool BankCardBalanceUpdate(string bankCardID, decimal toUpdateBalance)
        {
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 构建更新语句
                    string updateQuery = @"UPDATE ACCOUNT SET BALANCE = BALANCE + :toUpdateBalance 
                        WHERE CREDIT_CARD_NUMBERS = :bankCardID";
                    using (OracleCommand command = new OracleCommand(updateQuery, connection))
                    {
                        string newBalance = toUpdateBalance.ToString();
                        // 添加参数
                        command.Parameters.Add(":toUpdateBalance", OracleDbType.Varchar2).Value = newBalance;
                        command.Parameters.Add(":bankCardID", OracleDbType.Varchar2).Value = bankCardID;

                        // 执行更新操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public static string BankCardScoreInquiry(string bankCardID)
        {
            DataTable dataTable = BankCardInquiry(bankCardID);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                DataRow row = dataTable.Rows[0];
                string score = row["CREDIT_SCORE"].ToString();
                return score;
            }
            else
            {
                return "";
            }
        }

        // 银行卡查询
        public static DataTable BankCardInquiry(string bankCardID)
        {
            // 返回的表
            DataTable dataTable = new DataTable();

            // 创建数据库连接对象
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string selectQuery = "SELECT * FROM ACCOUNT WHERE CREDIT_CARD_NUMBERS = :bankCardID";
                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        OracleParameter param = new OracleParameter(":bankCardID", OracleDbType.Varchar2);
                        param.Value = bankCardID; // 替换成实际的银行卡号
                        command.Parameters.Add(param);

                        // 执行查询
                        using (OracleDataReader reader = command.ExecuteReader())
                        {
                            dataTable.Load(reader); // 将查询结果加载到 DataTable
                            return dataTable;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return new DataTable();
            }
        }

        public static bool CheckPassword(string passward, string card)
        {
            // 创建数据库连接对象
            String obpassword = "";
            try
            {
                using (OracleConnection connection = new OracleConnection(connectionString))
                {
                    connection.Open();

                    using (OracleCommand command = new OracleCommand("SELECT PASSWORD FROM ACCOUNT WHERE  CREDIT_CARD_NUMBERS = :account", connection))
                    {
                        command.CommandType = CommandType.Text;

                        // 定义输入参数
                        command.Parameters.Add("account", OracleDbType.Varchar2).Value = card;

                        // 执行查询
                        object result = command.ExecuteScalar();

                        // 处理结果
                        if (result != null && result != DBNull.Value)
                        {
                            obpassword = result.ToString();
                        }
                        else
                        {
                            return false;
                        }
                    }
                }

            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return PasswordHandler.VerifyPassword(passward, obpassword);
        }

        // 冻结、解冻、销户
        public static bool BankCardOperate(string bankCardID, OperateNumber operate)
        {
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();
                    string operateQuery = "";
                    string state = "";
                    operateQuery = @"UPDATE ACCOUNT SET STATE = :state 
                                            WHERE CREDIT_CARD_NUMBERS = :bankCardID";
                    switch (operate)
                    {
                        case OperateNumber.freeze:
                            state = "冻结";
                            break;
                        case OperateNumber.unfreeze:
                            state = "正常";
                            break;
                        case OperateNumber.accountClosure:
                            state = "已销户";
                            break;
                    }

                    // 构建更新语句
                    using (OracleCommand command = new OracleCommand(operateQuery, connection))
                    {
                        // 添加参数
                        if (state != "")
                        {
                            command.Parameters.Add(":state", OracleDbType.Varchar2).Value = state;
                        }
                        command.Parameters.Add(":bankCardID", OracleDbType.Varchar2).Value = bankCardID;

                        // 执行更新操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public static string GetBranchCardID(string branchID)
        {
            // 创建数据库连接对象
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string selectQuery = "SELECT BRANCH_CARD FROM BRANCH WHERE BRANCH_ID = :branchID";
                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        OracleParameter param = new OracleParameter(":branchID", OracleDbType.Varchar2);
                        param.Value = branchID; // 替换成实际的支行ID
                        command.Parameters.Add(param);

                        string cardID = (string)command.ExecuteScalar();
                        return cardID;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return "";
            }
        }

        public static DataTable GetTransferByCardID(string cardID)
        {
            // 返回的表
            DataTable dataTable = new DataTable();

            // 创建数据库连接对象
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string selectQuery = @"
SELECT * 
FROM TRANSACTION_HISTORY 
WHERE CREDIT_CARD_NUMBERS = :cardID1 
   OR ASSIST_CARD_NUMBERS = :cardID2"
;
                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        OracleParameter param = new OracleParameter(":cardID1", OracleDbType.Varchar2);
                        param.Value = cardID; // 替换成实际的银行卡号
                        command.Parameters.Add(param);

                        OracleParameter param1 = new OracleParameter(":cardID2", OracleDbType.Varchar2);
                        param1.Value = cardID; // 替换成实际的银行卡号
                        command.Parameters.Add(param1);

                        // 执行查询
                        using (OracleDataReader reader = command.ExecuteReader())
                        {
                            dataTable.Load(reader); // 将查询结果加载到 DataTable
                            return dataTable;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return new DataTable();
            }
        }

        public static DataTable GetDepositByCardID(string cardID)
        {
            // 返回的表
            DataTable dataTable = new DataTable();

            // 创建数据库连接对象
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string selectQuery = "SELECT * FROM TIME_DEPOSIT WHERE CREDIT_CARD_NUMBERS = :cardID";
                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        OracleParameter param = new OracleParameter(":cardID", OracleDbType.Varchar2);
                        param.Value = cardID; // 替换成实际的银行卡号
                        command.Parameters.Add(param);

                        // 执行查询
                        using (OracleDataReader reader = command.ExecuteReader())
                        {
                            dataTable.Load(reader); // 将查询结果加载到 DataTable
                            return dataTable;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return new DataTable();
            }
        }

        // 查询用户姓名
        public static string CustomerNameInquiryByID(string ID)
        {
            DataTable dataTable = CustomerInquiryByID(ID);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                DataRow row = dataTable.Rows[0];
                string name = row["NAME"].ToString();
                return name == null ? "" : name;
            }
            else
            {
                return "";
            }
        }

        // 使用ID进行用户查询
        public static DataTable CustomerInquiryByID(string ID)
        {
            // 返回的表
            DataTable dataTable = new DataTable();
            // 创建数据库连接对象
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string selectQuery = "SELECT * FROM PERSONAL_INFORMATION WHERE IDENTIFICATION_NUMBER = :ID";
                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        OracleParameter param = new OracleParameter(":ID", OracleDbType.Varchar2);
                        param.Value = ID; // 替换成实际的身份证号
                        command.Parameters.Add(param);

                        // 执行查询
                        using (OracleDataReader reader = command.ExecuteReader())
                        {
                            dataTable.Load(reader); // 将查询结果加载到 DataTable
                            return dataTable;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return new DataTable();
            }
        }

        public static DataTable CustomerInquiryByCardID(string cardID)
        {
            DataTable dataTable = new DataTable();
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();
                    // 数据库操作
                    string selectQuery = @"
SELECT * FROM PERSONAL_INFORMATION WHERE IDENTIFICATION_NUMBER = 
(SELECT IDENTIFICATION_NUMBER FROM ACCOUNT WHERE CREDIT_CARD_NUMBERS = :cardID)
";
                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        OracleParameter param = new OracleParameter(":cardID", OracleDbType.Varchar2);
                        param.Value = cardID;
                        command.Parameters.Add(param);

                        // 执行查询
                        using (OracleDataReader reader = command.ExecuteReader())
                        {
                            dataTable.Load(reader); // 将查询结果加载到 DataTable
                            return dataTable;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return new DataTable();
            }
        }

        // 使用其他属性分页筛选用户，过滤掉00000系统
        public static DataTable CustomerInquiry(string ID, string name, string address, string phoneNumber, int page, int pageSize)
        {

            DataTable dataTable = new DataTable();
            using (OracleConnection connection = new OracleConnection(connectionString))
            {
                connection.Open();
                int offset = (page - 1) * pageSize;
                string selectQuery = "SELECT * FROM PERSONAL_INFORMATION WHERE IDENTIFICATION_NUMBER <> '00000'";

                if (!string.IsNullOrEmpty(ID))
                {
                    selectQuery += "AND IDENTIFICATION_NUMBER LIKE :ID || '%'";
                }
                if (!string.IsNullOrEmpty(name))
                {
                    selectQuery += "AND NAME LIKE :name || '%'";
                }
                if (!string.IsNullOrEmpty(address))
                {
                    selectQuery += "AND ADDRESS LIKE :address || '%'";
                }
                if (!string.IsNullOrEmpty(phoneNumber))
                {
                    selectQuery += " AND PHONENUMBER LIKE :phoneNumber || '%'";
                }
                selectQuery += @"ORDER BY IDENTIFICATION_NUMBER
                       OFFSET :page ROWS
                       FETCH NEXT :pagesize  ROWS ONLY";

                using (OracleCommand command = new OracleCommand(selectQuery, connection))
                {
                    if (!string.IsNullOrEmpty(ID))
                    {
                        command.Parameters.Add(new OracleParameter("ID", OracleDbType.Varchar2) { Value = ID });
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        command.Parameters.Add(new OracleParameter("name", OracleDbType.Varchar2) { Value = name });
                    }
                    if (!string.IsNullOrEmpty(address))
                    {
                        command.Parameters.Add(new OracleParameter("address", OracleDbType.Varchar2) { Value = address });
                    }
                    if (!string.IsNullOrEmpty(phoneNumber))
                    {
                        command.Parameters.Add(new OracleParameter("phoneNumber", OracleDbType.Varchar2) { Value = phoneNumber });
                    }
                    command.Parameters.Add(new OracleParameter("page", (page - 1) * pageSize));
                    command.Parameters.Add(new OracleParameter("pagesize", pageSize));

                    using (OracleDataAdapter dataAdapter = new OracleDataAdapter(command))
                    {
                        try
                        {
                            dataAdapter.Fill(dataTable);

                        }
                        catch (Exception)
                        {
                            MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return new DataTable();
                        }
                    }
                }
            }
            return dataTable;
        }

        public static int GetCustomerNum(string ID, string name, string address, string phoneNumber)
        {
            try
            {
                DataTable dataTable = new DataTable();
                //建立连接
                using (OracleConnection connection = new OracleConnection(connectionString))
                {
                    connection.Open();
                    string selectQuery = "SELECT COUNT(*) FROM PERSONAL_INFORMATION WHERE IDENTIFICATION_NUMBER <> '00000'";

                    if (!string.IsNullOrEmpty(ID))
                    {
                        selectQuery += "AND IDENTIFICATION_NUMBER LIKE :ID || '%'";
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        selectQuery += "AND NAME LIKE :name || '%'";
                    }
                    if (!string.IsNullOrEmpty(address))
                    {
                        selectQuery += "AND ADDRESS LIKE :address || '%'";
                    }
                    if (!string.IsNullOrEmpty(phoneNumber))
                    {
                        selectQuery += " AND PHONENUMBER LIKE :phoneNumber || '%'";
                    }

                    using (OracleCommand command = new OracleCommand(selectQuery, connection))
                    {
                        if (!string.IsNullOrEmpty(ID))
                        {
                            command.Parameters.Add(new OracleParameter("ID", OracleDbType.Varchar2) { Value = ID });
                        }
                        if (!string.IsNullOrEmpty(name))
                        {
                            command.Parameters.Add(new OracleParameter("name", OracleDbType.Varchar2) { Value = name });
                        }
                        if (!string.IsNullOrEmpty(address))
                        {
                            command.Parameters.Add(new OracleParameter("address", OracleDbType.Varchar2) { Value = address });
                        }
                        if (!string.IsNullOrEmpty(phoneNumber))
                        {
                            command.Parameters.Add(new OracleParameter("phoneNumber", OracleDbType.Varchar2) { Value = phoneNumber });
                        }

                        int totalRecords = Convert.ToInt32(command.ExecuteScalar());
                        return totalRecords;
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return 0; // 出错返回0页
            }
        }

        // 用户插入
        public static bool CustomerInsert(string ID, string name, string address, string phoneNumber)
        {
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string insertQuery = @"
INSERT INTO PERSONAL_INFORMATION (IDENTIFICATION_NUMBER, NAME, ADDRESS, PHONENUMBER) 
VALUES (:ID, :name, :address, :phoneNumber)";
                    using (OracleCommand command = new OracleCommand(insertQuery, connection))
                    {
                        // 添加参数
                        command.Parameters.Add(":ID", OracleDbType.Varchar2).Value = ID;
                        command.Parameters.Add(":name", OracleDbType.Varchar2).Value = name;
                        command.Parameters.Add(":address", OracleDbType.Varchar2).Value = address;
                        command.Parameters.Add(":phoneNumber", OracleDbType.Varchar2).Value = phoneNumber;

                        // 执行插入操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        // 根据身份证号ID更新用户信息
        public static bool CustomerUpdate(string IDtoUpdate, string newName, string newAddress, string newPhoneNumber)
        {
            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 构建更新语句
                    string updateQuery = @"UPDATE PERSONAL_INFORMATION SET NAME = :newName, 
                        ADDRESS = :newAddress, 
                        PHONENUMBER = :newPhoneNumber
                        WHERE IDENTIFICATION_NUMBER = :IDtoUpdate";
                    using (OracleCommand command = new OracleCommand(updateQuery, connection))
                    {
                        // 添加参数
                        command.Parameters.Add(":newName", OracleDbType.Varchar2).Value = newName;
                        command.Parameters.Add(":newAddress", OracleDbType.Varchar2).Value = newAddress;
                        command.Parameters.Add(":newPhoneNumber", OracleDbType.Varchar2).Value = newPhoneNumber;
                        command.Parameters.Add(":IDtoUpdate", OracleDbType.Varchar2).Value = IDtoUpdate;

                        // 执行更新操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        // 银行卡办理
        public static string BankCardProcessing(string branchID, string ID, string name, string address, string phoneNumber, string InitialPassword)
        {
            if (CustomerInquiryByID(ID).Rows.Count == 0)
            {
                if (!CustomerInsert(ID, name, address, phoneNumber))
                {
                    return "";
                }
            }

            string bankCardID = BankAccountGenerator.GenerateUniqueCardID();
            string hashPassword = PasswordHandler.HashPassword(InitialPassword);
            DateTime openDate = GetCurrentTime();

            try
            {
                using (OracleConnection connection = new(connectionString))
                {
                    // 打开数据库连接
                    connection.Open();

                    // 数据库操作
                    string insertQuery = @"INSERT INTO ACCOUNT (CREDIT_CARD_NUMBERS, IDENTIFICATION_NUMBER, BRANCH_ID, OPEN_DATE, STATE, BALANCE, PASSWORD, CREDIT_SCORE) 
                                           VALUES (:bankCardID, :ID, :branchID, :openDate, :state, :balance, :hashPassword, :creditScore)";
                    using (OracleCommand command = new OracleCommand(insertQuery, connection))
                    {
                        // 添加参数
                        command.Parameters.Add(new OracleParameter("bankCardID", bankCardID));
                        command.Parameters.Add(new OracleParameter("ID", ID));
                        command.Parameters.Add(new OracleParameter("branchID", branchID));
                        command.Parameters.Add(new OracleParameter("openDate", openDate));
                        command.Parameters.Add(new OracleParameter("state", "正常"));
                        // 设置参数
                        OracleParameter param = new OracleParameter("balance", OracleDbType.Decimal);
                        param.Value = 0;
                        param.Size = 12;  // 总位数
                        param.Scale = 2;  // 小数点后位数

                        command.Parameters.Add(param);
                        command.Parameters.Add(new OracleParameter("hashPassword", hashPassword));
                        command.Parameters.Add(new OracleParameter("creditScore", 5000));

                        // 执行插入操作
                        if (command.ExecuteNonQuery() > 0)
                        {
                            return bankCardID;
                        }
                        else
                        {
                            return "";
                        }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("数据库连接失败，请检查网络连接！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return "";
            }
        }

        private static DateTime GetCurrentTime()
        {
            return DateTime.Now;
        }
    }

    // 随机生成20位的银行卡号
    public static class BankAccountGenerator
    {
        private static Random random = new Random();

        public static string GenerateUniqueCardID()
        {
            string cardNumber;
            do
            {
                cardNumber = GenerateRandom20ID();
            } while (AccountOperations.BankCardInquiry(cardNumber).Rows.Count > 0);

            return cardNumber;
        }

        public static string GenerateRandom20ID()
        {
            // 生成20位随机数字串
            const int cardNumberLength = 20;
            const string digits = "0123456789";
            char[] cardNumberArray = new char[cardNumberLength];

            for (int i = 0; i < cardNumberLength; i++)
            {
                cardNumberArray[i] = digits[random.Next(digits.Length)];
            }

            return new string(cardNumberArray);
        }
    }
}
